
package bsc.war.form;

/**
 *
 * @author castrosalazar
 */

import bsc.ejb.entity.SRM;
import bsc.ejb.entity.SRMTopic;
import bsc.ejb.entity.TopicScoreView;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.BSCUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class UpdateSRMTopic extends AbstractController {

    public UpdateSRMTopic() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idSRMTopic = request.getParameter("srmtopic") != null && NumberUtils.isNumber(request.getParameter("srmtopic")) ? Long.parseLong(request.getParameter("srmtopic")) : null;
            SRMTopic SRMTopic = delegate.findSRMTopic(idSRMTopic);
            TopicScoreView topicScoreView = delegate.findTopicScoreView(SRMTopic.getTopic(), SRMTopic.getSRM().getScoreView());
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            
            if(request.getParameter("send")!=null & request.getParameter("delete")==null) {
                String deal = request.getParameter("deal") != null && !request.getParameter("deal").isEmpty() ?
                    request.getParameter("deal") : null;
                String type = request.getParameter("type") != null && !request.getParameter("type").isEmpty() ?
                    request.getParameter("type") : null;
                Double expectedTime = request.getParameter("expectedTime") != null && NumberUtils.isNumber(request.getParameter("expectedTime")) ? Double.parseDouble(request.getParameter("expectedTime")) : null;
                Double progress = request.getParameter("progress") != null && NumberUtils.isNumber(request.getParameter("progress")) ? Double.parseDouble(request.getParameter("progress")) : null;
                String responsable = request.getParameter("responsable") != null && !request.getParameter("responsable").isEmpty() ?
                    request.getParameter("responsable") : null;
                String date = request.getParameter("date") != null && !request.getParameter("date").isEmpty() ?
                    request.getParameter("date") : null;
                String description = request.getParameter("description") != null && !request.getParameter("description").isEmpty() ?
                    request.getParameter("description") : null;

                SRMTopic.setProgress(progress);
                if(date != null) SRMTopic.setNextMeeting(df.parse(date));
                SRMTopic.setDeal(deal);
                SRMTopic.setExpectedTime(expectedTime);
                SRMTopic.setResponsable(responsable);
                SRMTopic.setDescription(description);
                delegate.updateSRMTopic(SRMTopic);

                topicScoreView.setType(TopicScoreView.Type.valueOf(type));
                delegate.updateTopicScoreView(topicScoreView);
            }
            else
            {
                request.setAttribute("deleted","yeah!");
                delegate.deleteSRMTopic(SRMTopic);
            }

            SRM srm = SRMTopic.getSRM();
            //srm.setExpectedTime(BSCUtils.getExpectedTime(delegate, srm));
            delegate.updateSRM(srm);
            
            request.setAttribute("SRMTopic",SRMTopic);
            request.setAttribute("topicScoreView",topicScoreView);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Caught exception: ", e);
        }
        return new ModelAndView("updateSRMTopic");
    }
}
